{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From D:\\Program Files\\anaconda\\lib\\site-packages\\tensorflow\\python\\compat\\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "non-resource variables are not supported in the long term\n",
      "WARNING:tensorflow:From D:\\Program Files\\anaconda\\lib\\site-packages\\tensorflow\\python\\util\\tf_should_use.py:247: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.\n",
      "Instructions for updating:\n",
      "Use `tf.global_variables_initializer` instead.\n",
      "0.08891171\n",
      "0.009704986\n",
      "0.0071189716\n",
      "0.0052653537\n",
      "0.0045632734\n",
      "0.004247971\n",
      "0.004042794\n",
      "0.003896034\n",
      "0.0037883949\n",
      "0.0037056128\n",
      "0.0036406883\n"
     ]
    }
   ],
   "source": [
    "#coding=utf-8\n",
    "\n",
    "import tensorflow.compat.v1 as tf\n",
    "tf.disable_v2_behavior()\n",
    "import numpy as np\n",
    " \n",
    "#add layer\n",
    "def add_layer(inputs,in_size,out_size,activation_function=None):\n",
    "\t# add one more layer and return the output of this layer\n",
    "\tw = tf.Variable(tf.random_normal([in_size,out_size]))\n",
    "\tb = tf.Variable(tf.zeros([1,out_size])+0.1)\n",
    "\ty = tf.matmul(inputs,w)+b\n",
    "\tif activation_function is None:\n",
    "\t\toutputs = y\n",
    "\telse:\n",
    "\t\toutputs = activation_function(y)\n",
    "\treturn outputs\n",
    " \n",
    "#make train data\n",
    "x_data = np.linspace(-1,1,300)[:,np.newaxis]\n",
    "noise = np.random.normal(0,0.05,x_data.shape)\n",
    "y_data = np.square(x_data) - 0.5 + noise\n",
    "#print (x_data)\n",
    "#print (y_data)\n",
    "#define placeholder\n",
    "xs = tf.placeholder(tf.float32,[None,1])\n",
    "ys = tf.placeholder(tf.float32,[None,1])\n",
    "#add hidden layer\n",
    "l1 = add_layer(xs,1,10,activation_function = tf.nn.relu)\n",
    "#add output layer\n",
    "prediction = add_layer(l1,10,1,activation_function = None)\n",
    " \n",
    "#define loss function\n",
    "loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices = [1]))\n",
    "#定义用什么方法减少loss\n",
    "optimizer = tf.train.GradientDescentOptimizer(0.1).minimize(loss)\n",
    " \n",
    "with tf.Session() as sess:\n",
    "\tsess.run(tf.initialize_all_variables())\n",
    "\tfor i in range(1001):\n",
    "\t\tsess.run(optimizer,feed_dict = {xs:x_data,ys:y_data})\n",
    "\t\tif i%100 == 0:\n",
    "\t\t\tprint (sess.run(loss,feed_dict = {xs:x_data,ys:y_data}))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
